import Mock from 'mockjs';
import qs from 'qs';
import { BaseUrl } from '@/api/const';

const base = '/books';

function productData(limit: number) {
  return Mock.mock({
    code: 0,
    message: '',
    total: 100,
    data: function () {
      let result = [];
      for (let i = 0; i < limit; i++) {
        result.push(productDataItem());
      }
      return result;
    }
  });
}

function productDataItem() {
  return Mock.mock({
    id: /[a-zA-Z0-9]{10}/,
    time: new Date(),
    type: 'outlay',
    category: /购物|吃饭|交通/,
    'amount|1-50': 1,
    mark: 'xxxx'
  });
}

// 拦截带参数请求，需用正则匹配路径
function regUrl(url: string) {
  return RegExp(BaseUrl() + url + '.*');
}

// ?current=1&limit=10
Mock.mock(regUrl(`${base}/list`), 'get', (options) => {
  const params: any = qs.parse(options.url.split('?')[1]);
  console.log(params);
  return productData(params.limit);
});
